/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.commons.util;

/**
 @author Roy
 @version v2.0
15位主键生成工具
 使用方法：UUID.GenUUID15()

 */

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.StringTokenizer;

public class UUID
{
    public static final char Chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    public static final char Nums[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public static final char OnlyChars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    public static String GenUUID()
    {
        String result = null;

        //1位产生方法
        String HWay = "a";

        //8位IP
        String HIP = null;
        String DefalultIP = "FFFFFFFF";
        try
        {
            InetAddress Local = InetAddress.getLocalHost();
            HIP = Local.getHostAddress();
            StringTokenizer st = new StringTokenizer(HIP, ".");
            HIP = Long.toHexString(java.lang.Integer.parseInt(st.nextToken()) * Math.round(Math.pow(2, 24)) + java.lang.Integer.parseInt(st.nextToken()) * Math.round(Math.pow(2, 16)) + java.lang.Integer.parseInt(st.nextToken()) * Math.round(Math.pow(2, 8)) + java.lang.Integer.parseInt(st.nextToken()));
            if (HIP.length() < 8)
            {
                HIP = "0" + HIP;
            }
        }
        catch (UnknownHostException e)
        {
            HIP = DefalultIP;
        }

        //12位时间
        String HNow = Long.toHexString(new Date().getTime());
        while (HNow.length() < 12)
        {
            HNow = "0" + HNow;
        }
        while (HNow.length() > 12)
        {
            HNow = HNow.substring(HNow.length() - 12, HNow.length());
        }

        //4位随机数
        char CRandom[] = new char[4];
        CRandom[0] = RandomChar();
        CRandom[1] = RandomChar();
        CRandom[2] = RandomChar();
        CRandom[3] = RandomChar();
        String HRandom = String.valueOf(CRandom);

        //3位没用，先用随机数填充
        char CRandom2[] = new char[3];
        CRandom2[0] = RandomChar();
        CRandom2[1] = RandomChar();
        CRandom2[2] = RandomChar();
        String HNouse = String.valueOf(CRandom2);

        result = HWay + HIP + HNow + HRandom + HNouse;

        //二位校验码
        byte HCheck = (byte) result.charAt(0);
        for (int tempint = 1; tempint < result.length(); tempint++)
        {
            HCheck = (byte) ((byte) result.charAt(tempint) ^ (byte) HCheck);
        }
        if (HCheck <= 0xf)
        {
            result += "0" + java.lang.Integer.toHexString(HCheck);
        }
        else
        {
            result += java.lang.Integer.toHexString(HCheck);
        }

        return result;
    }

    private static final char RandomChar()
    {
        char result = Chars[((int) (Math.random() * java.lang.Integer.MAX_VALUE)) % Chars.length];//10个数字，52个字母
        return result;
    }

    public static String GenUUID15()
    {
        StringBuffer sb = new StringBuffer(15);
        //1位产生方法
        sb.append('a');
        //10位时间
        String HNow = Long.toString(new Date().getTime(), Character.MAX_RADIX);
        if (HNow.length() > 10)
        {
            HNow = HNow.substring(HNow.length() - 10, HNow.length());
            sb.append(HNow);
        }
        if (HNow.length() < 10)
        {
            for (int i = 0; i < 10 - HNow.length(); i++)
            {
                sb.append('A');
            }
            sb.append(HNow);
        }
        //3位随机数
        sb.append(RandomChar());
        sb.append(RandomChar());
        sb.append(RandomChar());
        //1位效验码
        byte HCheck = (byte) sb.charAt(0);
        for (int tempint = 1; tempint < sb.length(); tempint++)
        {
            HCheck = (byte) ((byte) sb.charAt(tempint) ^ (byte) HCheck);
        }
        sb.append(Chars[HCheck % Chars.length]);
        return sb.toString();
    }

    public static String randNums(int _length)
    {
        if(_length <= 0)
        {
            return null;
        }
        StringBuffer sb = new StringBuffer(_length);
        for(int i = 0; i < _length; i ++)
        {
            sb.append(Nums[((int) (Math.random() * java.lang.Integer.MAX_VALUE)) % Nums.length]);
        }

        return sb.toString();
    }

    public static String randChars(int _length)
    {
        if(_length <= 0)
        {
            return null;
        }
        StringBuffer sb = new StringBuffer(_length);
        for(int i = 0; i < _length; i ++)
        {
            sb.append(OnlyChars[((int) (Math.random() * java.lang.Integer.MAX_VALUE)) % OnlyChars.length]);
        }

        return sb.toString();
    }
}
